package com.qzsoft.zhjd.machine.modules.machineinfo.entity;

import java.math.BigDecimal;
import java.util.Date;

import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import org.springframework.format.annotation.DateTimeFormat;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.qzsoft.jeemis.common.annotation.Dict;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @ClassName MachineInfoEntity
 * @Description 机械设备信息
 * @author linhuawei
 * @since 2021-11-01
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
@TableName("machine_info")
public class MachineInfoEntity {

    /**
     * 设备主键
     */
    @TableId(type = IdType.ASSIGN_UUID)
    private String id;

    /**
     * 单位编码
     */
    @NotBlank(groups = {AddGroup.class},message = "所属单位不能为空！")
    @Length(groups = {AddGroup.class},max = 36,message = "单位编码不能超过{max}个字")
    private String deptId;

    /**
     * 单位主键
     */
    private String deptPkid;

    /**
     * 单位名称
     */
    @TableField(exist = false)
    @Dict(dicCodeField = "deptId", dictCodeId = "UN")
    private String deptName;

    /**
     * 设备名称
     */
    @NotBlank(groups = {UpdateGroup.class, AddGroup.class},message = "设备名称不能为空！")
    private String name;
    
    /**
     * 资产编号
     */
    @Length(groups = {AddGroup.class,UpdateGroup.class},max = 36,message = "资产编号不可超过{max}个字符")
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private String assetNumber;

    /**
     * 设备编码
     */
    @Length(groups = {AddGroup.class,UpdateGroup.class},max = 36,message = "设备编码不可超过{max}个字符")
    private String machineKey;

    /**
     * 设备类型
     */
    @NotBlank(groups = {AddGroup.class,UpdateGroup.class},message = "设备类型不可为空")
    @Length(groups = {AddGroup.class,UpdateGroup.class},max = 36,message = "设备类型不可超过{max}个字符")
    private String type;

    /**
     * 设备类型
     */
    @Dict(dicCodeField = "type", dictCodeId = "MACHINE_TYPE")
    @TableField(exist = false)
    private String typeName;

    /**
     * 设备一级分类
     */
    @NotBlank(groups = {AddGroup.class,UpdateGroup.class},message = "设备一级分类不可为空")
    @Length(groups = {AddGroup.class,UpdateGroup.class},max = 36,message = "设备一级分类不可超过{max}个字符")
    private String category1;

    /**
     * 设备一级分类名称
     */
    @Dict(dicCodeField = "category1", dictCodeId = "MACHINE_CATEGORY")
    @TableField(exist = false)
    private String category1Name;

    /**
     * 设备二级分类
     */
    @NotBlank(groups = {AddGroup.class,UpdateGroup.class},message = "设备二级分类不可为空")
    @Length(groups = {AddGroup.class,UpdateGroup.class},max = 36,message = "设备二级分类不可超过{max}个字符")
    private String category2;

    /**
     * 设备二级分类名称
     */
    @Dict(dicCodeField = "category2", dictCodeId = "MACHINE_CATEGORY")
    @TableField(exist = false)
    private String category2Name;

    /**
     * 设备品牌
     */
    @Length(groups = {AddGroup.class,UpdateGroup.class},max = 36,message = "设备品牌不可超过{max}个字符")
    private String brand;

    /**
     * 设备品牌名称
     */
    @Dict(dicCodeField = "brand", dictCodeId = "MACHINE_BRAND_MODEL")
    @TableField(exist = false)
    private String brandName;

    /**
     * 设备型号
     */
    @NotBlank(groups = {AddGroup.class,UpdateGroup.class},message = "设备型号不可为空")
    @Length(groups = {AddGroup.class,UpdateGroup.class},max = 36,message = "设设备型号不可超过{max}个字符")
    private String model;

    /**
     * 设备型号名称
     */
    @Dict(dicCodeField = "model", dictCodeId = "MACHINE_BRAND_MODEL")
    @TableField(exist = false)
    private String modelName;

    /**
     * 发动机功率
     */
    @Range(groups = {AddGroup.class,UpdateGroup.class}, min = 0, max = 9999999, message = "发动机功率取值{min}~{max}")
    private BigDecimal enginePower;

    /**
     * 生产厂家
     */
    @Length(groups = {AddGroup.class,UpdateGroup.class},max = 200,message = "生产厂家不可超过{max}个字符")
    private String manufacturer;

    /**
     * 资产原值
     */
    @NotNull(groups = {AddGroup.class,UpdateGroup.class},message = "资产原值不可为空！")
    @Range(groups = {AddGroup.class,UpdateGroup.class}, min = 0, max = 9999999, message = "资产原值取值{min}~{max}")
    private BigDecimal originalValue;

    /**
     * 每月折旧金额
     */
    private BigDecimal deprMonthValue;

    /**
     * 开始折旧日期
     */
    @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM")
    private Date deprStartDate;

    /**
     * 最大折旧月份
     */
    @NotNull(groups = {AddGroup.class,UpdateGroup.class},message = "最大折旧月份不可为空！")
    @Range(groups = {AddGroup.class,UpdateGroup.class}, min = 0, max = 1000, message = "最大折旧月份{min}~{max}")
    private Integer deprMaxMonth;

    /**
     * 开始使用日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date startUseDate;

    /**
     * 使用状态
     */
    private String useState;

    /**
     * 使用状态
     */
    @Dict(dicCodeField = "useState", dictCodeId = "MACHINE_USE_STATE")
    @TableField(exist = false)
    private String useStateName;

    /**
     * 技术状态
     */
    @Length(groups = {AddGroup.class,UpdateGroup.class},max = 36,message = "技术状态不可超过{max}个字符")
    private String technologyState;

    /**
     * 技术状态
     */
    @Dict(dicCodeField = "technologyState", dictCodeId = "MACHINE_TECHNOLOGY_STATE")
    @TableField(exist = false)
    private String technologyStateName;

    /**
     * 使用责任人
     */
    @Length(groups = {AddGroup.class,UpdateGroup.class},max = 36,message = "使用责任人不可超过{max}个字符")
    private String contactPerson;

    /**
     * 联系电话
     */
    @Length(groups = {AddGroup.class,UpdateGroup.class},max = 36,message = "联系电话不可超过{max}个字符")
    private String contactPhone;

    /**
     * 备注信息
     */
    @Length(groups = {AddGroup.class,UpdateGroup.class},max = 200,message = "备注信息不可超过{max}个字符")
    private String info;

    /**
     * 设备图片
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String coverImg;

    /**
     * 正前方图片
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String frontImg;

    /**
     * 侧方图片
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String sideImg;

    /**
     * 正后方图片
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String behindImg;

    /**
     * 前侧方45°图片
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String frontSideImg;

    /**
     * 合格证图片
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String certificateImg;

    /**
     * 发票图片
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String invoiceImg;

    /**
     * 铭牌图片
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String nameplateImg;

    /**
     * 驾驶证图片
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String drivingImg;

    /**
     * 驾驶证有效期
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date drivingValidity;

    /**
     * 商业保险图片
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String insuranceImg;

    /**
     * 商业保险有效期
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date insuranceValidity;

    /**
     * 创建者
     */
    @TableField(fill = FieldFill.INSERT, updateStrategy = FieldStrategy.NEVER)
    private Long creator;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT, updateStrategy = FieldStrategy.NEVER)
    private Date createDate;

    /**
     * 更新者
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updater;

    /**
     * 更新时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateDate;

//    /**
//     * 累计折旧月份
//     */
//    @TableField(exist = false)
//    private Integer deprTotalMonth;
//
//    /**
//     * 累计折旧金额
//     */
//    @TableField(exist = false)
//    private BigDecimal deprTotalValue;
//
//    /**
//     * 资产净值
//     */
//    @TableField(exist = false)
//    private BigDecimal netValue;
//
//    /**
//     * 本月油耗
//     */
//    @TableField(exist = false)
//    private BigDecimal fuelConsume;
//
//    /**
//     * 本月台班
//     */
//    @TableField(exist = false)
//    private BigDecimal taiban;
//
//    public void count() {
//        /* 计算折旧相关 */
//        // 累计折旧月份
//        if (null != deprStartDate) {
//            // 获取当前年份、月份
//            Calendar cal = Calendar.getInstance();
//            int endMonth = cal.get(Calendar.MONTH) + 1;
//            int endYear = cal.get(Calendar.YEAR);
//            
//
//            // 获取开始折旧年份、月份
//            cal.setTime(deprStartDate);
//            int startMonth = cal.get(Calendar.MONTH) + 1;
//            int startYear = cal.get(Calendar.YEAR);
//            
//            
//            // 累计折旧月份（不包含当前月份）
//            deprTotalMonth = (endYear * 12 + endMonth) - (startYear * 12 + startMonth);
//            if(deprTotalMonth > deprMaxMonth) {
//                deprTotalMonth = deprMaxMonth;
//            }
//            
//            // 累计折旧金额
//            deprTotalValue = deprMonthValue.multiply(new BigDecimal(deprTotalMonth));
//            
//            // 净值
//            netValue = originalValue.subtract(deprTotalValue);
//
//        }
//    }

}
